package com.yanqu.road.server.cmd.activity.cat;

import com.yanqu.road.entity.activity.cat.data.CatUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CatProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.cat.CatModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.cat.CatMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CAT_ITEM_USE, desc = "道具恢复体力")
public class CatUseEnergyItemCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        if (!ActivityMgr.activityInTime(CatMgr.getActivityInfo()) || CatMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        CatProto.CatUseEnergyItemReqMsg reqMsg = CatProto.CatUseEnergyItemReqMsg.parseFrom(packet.getBytes());

        ret = player.getModule(CatModule.class).useEnergyItem(reqMsg.getCount());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            CatProto.CatUseEnergyItemRespMsg.Builder respMsg = CatProto.CatUseEnergyItemRespMsg.newBuilder();
            CatUserData userData = player.getModule(CatModule.class).getUserData();
            if (userData != null){
                respMsg.setEnergy(userData.getEnergy());
                respMsg.setLastRecTime(userData.getLastRecTime());
            }
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CAT_ITEM_USE, respMsg);
        }
    }
}
